package top.littlejiang.onlinexam.common.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import top.littlejiang.onlinexam.common.configuration.RedisConfig;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Set;

/**
 * @author chen
 * @Description
 * @since 2020/12/30 10:58
 */
@Slf4j
@Component
public class RedisUtils {

    /**
     * 验证码存储前缀
     */
    public static String CODE_PREFIX = "valicode_";
    /**
     * 验证码有效时间 单位：秒
     */
    public static int CODE_TIME = 3 * 60;
    /**
     * 登录信息存储前缀
     */
    public static String LOGIN_PREFIX = "userLogin_";

    private static JedisPool jedisPool;

    @Autowired
    private RedisConfig redisConfig;

    @PostConstruct
    public void init() {
        jedisPool = redisConfig.redisPoolFactory();
    }


    //==================== string ======================================
    /**
     * 根据 key 查找 String
     * @param key
     * @return: String
     * @author chen
     * @since 2020/12/30 14:49
     */
    public static String getStr(String key){
        Jedis jedis = null;
        String value = null;
        try {
            //获取一个jedis实例
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            log.error("RedisUtils-getStr错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return value;

    }

    /**
     * 不覆盖增加数据项（重复不插入）
     * @param  key
     * @param  value
     * @return: boolean
     * @author chen
     * @since 2020/12/30 18:27
     */
    public static boolean setnx(String key, String value){
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            jedis.setnx(key, value);
            flag = true;
        } catch (Exception e) {
            log.error("RedisUtils-setnx错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return flag;
    }

    /**
     * 增加数据项
     * @param  key
     * @param  value
     * @return: boolean
     * @author chen
     * @since 2020/12/30 18:36
     */
    public static boolean setString(String key,String value){
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            flag = true;
        }catch (Exception e){
            log.error("RedisUtils-setString错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return flag;
    }

    /**
     * 增加数据项并设置过期时间
     * @param  key
     * @param  t
     * @param  value
     * @return: boolean
     * @author chen
     * @since 2020/12/30 18:36
     */
    public static boolean setex(String key,int t,String value){
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            jedis.setex(key,t,value);
            flag = true;
        }catch (Exception e){
            log.error("RedisUtils-setex错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return flag;
    }

    /**
     * 删除单个 key 对应的数据
     * @param  key
     * @return: boolean
     * @author chen
     * @since 2020/12/30 18:30
     */
    public static boolean delKey (String key){
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
            flag = true;
        }catch (Exception e){
            log.error("RedisUtils-delKey错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return flag;
    }

    /**
     * 删除多个 key
     * @param  keys
     * @return: boolean
     * @author chen
     * @since 2020/12/30 18:32
     */
    public static boolean delKeys(String...keys){
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            jedis.del(keys);
            flag = true;
        }catch (Exception e){
            log.error("RedisUtils-delKeys错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return flag;
    }

    //==================== hash ======================================

    /**
     * 将map添加为一个hash
     * @param  key
     * @param  map
     * @return: boolean
     * @author chen
     * @since 2020/12/30 18:48
     */
    public static boolean hmset (String key, Map map){
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            jedis.hmset(key,map);
            flag = true;
        }catch (Exception e){
            log.error("RedisUtils-hmset错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return flag;
    }

   /**
    * 向一个hash表中添加一条数据
    * @param hash
    * @param key
    * @param value
    * @return boolean
    * @author chen
    * @since 2021/1/2 11:58
    */
    public static boolean hset(String hash,String key,String value){
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            jedis.hset(hash,key,value);
            flag = true;
        }catch (Exception e){
            log.error("RedisUtils-hset错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return flag;
    }

    /**
     * 获取 hash 中的所有 key
     * @param hash
     * @return boolean
     * @author chen
     * @since 2021/4/10 14:55
     */
    public static Set<String> hkeys(String hash){
        Jedis jedis = null;
        Set<String> keys = null;
        try {
            jedis = jedisPool.getResource();
            keys = jedis.hkeys(hash);
        }catch (Exception e){
            log.error("RedisUtils-hkeys错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return keys;
    }

    /**
     * 从一个hash中获取一条数据
     * @param  hash
     * @param  key
     * @return: String
     * @author chen
     * @since 2020/12/31 12:31
     */
    public static String hget(String hash,String key){
        Jedis jedis = null;
        String value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.hget(hash,key);
        }catch (Exception e){
            log.error("RedisUtils-hget错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 删除 hash中数据
     * @param  hash
     * @param  keys
     * @return: boolean
     * @author chen
     * @since 2020/12/31 12:38
     */
    public static boolean hdel(String hash,String...keys){
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            jedis.hdel(hash,keys);
            flag = true;
        }catch (Exception e){
            log.error("RedisUtils-hdel错误日志："+ e.getMessage());
        } finally {
            jedis.close();
        }
        return flag;
    }

}
